

layui.use(['table', 'HttpRequest', 'func', 'form'], function () {



    var $ = layui.$;
    var table = layui.table;
    var HttpRequest = layui.HttpRequest;
    var func = layui.func;
    var form = layui.form;

    /**
     * 初始化参数
     */
    var Device = {
        tableId: "DeviceTable"
    };

    /**
     * 初始化表格的列
     */
    Device.initColumn = function () {
        return [[
            {type: 'radio'},
            {field: 'id', sort: true, title: 'ID'},
            {field: 'projectId', sort: true, align: "center", title: '项目编号'},
            {field: 'projectName', sort: true, align: "center", title: '项目名称'},
            {field: 'deviceBrand', sort: true, align: "center", title: '设备厂家'},
            {
                field: 'deviceType', sort: true, align: "center", title: '设备类型', templet: function (data) {
                    if (data.deviceType === 1) {
                        return '塔吊';
                    } else if(data.deviceType === 2){
                        return '升降机';
                    }else if(data.deviceType === 3){
                        return '吊钩可视化';
                    }else if(data.deviceType === 4){
                        return 'Al服务器';
                    }else if(data.deviceType === 5){
                        return '监控摄像头';
                    }else if(data.deviceType === 6){
                        return '电检测设备';
                    }
                }
            },
            {field: 'deviceSn', sort: true, align: "center", title: '设备编号'},
            {field: 'deviceLastTime', sort: true, align: "center", title: '最后在线时间',templet:function (data) {
                    return timetrans(data.deviceLastTime);
                }},
            {field: 'status', sort: true, align: "center", title: '状态', templet: function (data) {
                    if (data.status === 0) {
                        return '未启用';
                    } else if(data.status === 1){
                        return '启用';
                    }
                }},
            {field: 'createTime', sort: true, align: "center", title: '入库时间', templet:function (data) {
                    // return util.toDateString(data.createTime*1000);
                    return timetrans(data.createTime)
                }},
            {field: 'lastTime', sort: true, align: "center", title: '最后更新时间'},
            {field: 'remarks', sort: true, align: "center", title: '备注'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    function timetrans (res) {
        // 时间戳为0的情况下util.toDateString转换失败
        var date = new Date(res*1000);//如果date为13位不需要乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
        var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        var m = (date.getMinutes() <10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        var s = (date.getSeconds() <10 ? '0' + date.getSeconds() : date.getSeconds());
        return Y+M+D+h+m+s;
    };



    /**
     * 点击查询按钮
     */
    Device.search = function () {
        var queryData = {};
        queryData['deviceSn'] = $("#projectId").val();
        queryData['projectName'] = $("#projectName").val();
        queryData['status']  = $("#status").val();
        table.reload(Device.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 点击重置按钮
     */
    Device.Reset = function () {

        $("#status").val("");
        $("#projectId").val("");
        $("#projectName").val("");
        form.render();
        table.reload(Device.tableId, {
           where: null, page: {curr: 1}
        });
    };

    /**
     * 添加设备配置对话框
     */
    Device.openAddDlg = function () {
        func.open({
            title: '添加设备配置',
            content: Feng.ctxPath + '/deviceconfig/manage/add',
            tableId: Device.tableId
        });
    };

    // 下拉框和统计数据
    layui.use(['form', 'upload', 'layer'],function(){
        statistics();
        statusDict();
    });

    function statusDict() {
        $.ajax({
            url: Feng.ctxPath + '/dict/getDictListPage',
            data:{page:"1",limit:"10",dictTypeCode:"deviceStatus"},
            dataType: 'json',
            type: 'get',
            success: function (data) {
                console.log(data);//下面会提到这个data是什么值
                //使用循环遍历，给下拉列表赋值
                $.each(data.data.rows, function (index, value) {
                    // console.log(value.department_id);
                    $('#status').append(new Option(value.dictName,value.dictCode));// 下拉菜单里添加元素
                });
                layui.form.render("select");//重新渲染 固定写法
            }
        })
    }


    function statistics() {
        $.ajax({
            url: Feng.ctxPath + '/device/statistics',
            dataType: 'json',
            type: 'get',
            success: function (data) {
                console.log(data);//下面会提到这个data是什么值

                $("#towenCount").html(data.data.towenCount);
                $("#liftCount").html(data.data.liftCount);
                $("#hookCount").html(data.data.hookCount);
                $("#alCount").html(data.data.alCount);
                $("#monitorCount").html(data.data.monitorCount);
                $("#eleCount").html(data.data.eleCount);
            }
        })
    }




    /**
     * 编辑设备配置对话框
     *
     * @param data 点击按钮时候的行数据
     */
    Device.openEditDlg = function (data) {
        func.open({
            title: '编辑设备配置',
            content: Feng.ctxPath + '/deviceconfig/manage/edit?id=' + data.id,
            tableId: Device.tableId
        });
    };


    $('#projectId').keyup(function(event){
        if(event.keyCode ==13){
            $("#btnSearch").trigger("click");
        }
    });

    //监听行双击事件
    table.on('rowDouble(DeviceTable)', function(obj){
        //obj 同上
        console.log(obj)
        func.open({
            title: '查看设备配置',
            content: Feng.ctxPath + '/deviceconfig/manage/rowDouble?id=' + obj.data.id,
            tableId: Device.tableId
        });

    });


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Device.tableId,
        url: Feng.ctxPath + '/deviceconfig/findPage',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Device.initColumn(),
        request: {pageName: 'pageNo', limitName: 'pageSize'},
        parseData: Feng.parseData
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Device.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Device.openAddDlg();
    });

    // 点击重置按钮
    $('#btnReset').click(function () {
        Device.Reset();
    });

    // 工具条点击事件
    table.on('tool(' + Device.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Device.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Device.onDeleteItem(data);
        }
    });

});
